import sys
import cv2
from PySide6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PySide6.QtGui import QPixmap, QImage


class ImageViewer(QWidget):
    def __init__(self, image_path):
        super().__init__()
        self.setWindowTitle("PySide6 + OpenCV 显示图片")

        # 加载图片
        image = cv2.imread(image_path)
        image.resize()
        if image is None:
            raise FileNotFoundError(f"无法读取图片：{image_path}")

        y, x, ch1 = image.shape
        print(f"high:{y},{x},{ch1}")

        view = image.data
        # print(f"{view.}")
        reimage = cv2.resize(image, (int(x / 4), int(y / 2)))

        image_rgb = cv2.cvtColor(reimage, cv2.COLOR_BGR2RGB)
        h, w, ch = image_rgb.shape
        bytes_per_line = ch * w


        qt_image = QImage(image_rgb.data, w, h, bytes_per_line, QImage.Format_RGB888)

        # 创建 QLabel 显示图片
        self.label = QLabel()
        self.label.setPixmap(QPixmap.fromImage(qt_image))


        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)


# if __name__ == "__main__":
#     app = QApplication(sys.argv)
#
#     # ✅ 在这里改成你自己的图片路径（绝对路径或相对路径）
#     image_path = "E:\\wifi\\asdasd.png"
#
#     window = ImageViewer(image_path)
#     window.show()
#     sys.exit(app.exec())
